package com.situ.achievementsystem.department.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.mysql.cj.xdevapi.JsonArray;
import com.situ.achievementsystem.department.dao.DepartmentDao;
import com.situ.achievementsystem.department.model.DepartmentModel;
import com.situ.achievementsystem.department.service.DepartmentService;
import com.sun.org.apache.bcel.internal.generic.NEW;

import sun.print.resources.serviceui;

/**
 * Servlet implementation class DepartmentServlet
 */
@WebServlet("/DepartmentServlet")
public class DepartmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DepartmentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
//    	response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		Object res = "";
		DepartmentModel model;
		switch (action) {
		case "list":
			model = new DepartmentModel(request.getParameter("code"),request.getParameter("name"));
			res = service.selectList(model);
			res = new JSONArray((Collection<?>)res).toString();
//			List<DepartmentModel> list = new ArrayList<>();
//			list.add(model);
//			System.out.println(model);
//			System.out.println(res);
			break;
			
		case "add":
			model = new DepartmentModel(request.getParameter("code"),request.getParameter("name"),request.getParameter("del"));
//			System.out.println(model);
			res = service.insert(model);
//			System.out.println(res);
			break;
			
		case "del":
			model = new DepartmentModel(request.getParameter("code"));
			res =service.delete(model);
			break;
			
		case "upd":
			model = new DepartmentModel(request.getParameter("code"),request.getParameter("name"),request.getParameter("del"));
			res = service.update(model);
			break;
			
		case "get":
			model = new DepartmentModel(request.getParameter("code"));
			res = service.selectModel(model);
//			res = new JSONArray((Collection<?>)res).toString();
			res = new JSONObject(res).toString();
//			System.out.println(res);
			break;
			
		default:
			break;
		}
		
		PrintWriter wr = response.getWriter();
//		new JSONObject(res)
		wr.print(res);
		wr.flush();
		wr.close();
		wr=null;
	}



	private DepartmentService service = new DepartmentService();

}
